{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.6/site-packages/sklearn/cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.\n",
      "  \"This module will be removed in 0.20.\", DeprecationWarning)\n"
     ]
    }
   ],
   "source": [
    "import malaya\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "news_sentiment = malaya.pretrained_bayes_sentiment()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "positive_text = 'Kerajaan negeri Kelantan mempersoalkan motif kenyataan Menteri Kewangan Lim Guan Eng yang hanya menyebut Kelantan penerima terbesar bantuan kewangan dari Kerajaan Persekutuan. Sedangkan menurut Timbalan Menteri Besarnya, Datuk Mohd Amar Nik Abdullah, negeri lain yang lebih maju dari Kelantan turut mendapat pembiayaan dan pinjaman.'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'positive'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "news_sentiment.predict(positive_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'negative': 0.2341921505296387, 'positive': 0.7658078494703627}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "news_sentiment.predict(positive_text,get_proba=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'negative': 0.19026157818306375, 'positive': 0.8097384218169362}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "negative_text = 'kerajaan sebenarnya sangat bencikan rakyatnya, minyak naik dan segalanya'\n",
    "news_sentiment.predict(negative_text,get_proba=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'negative': 0.19026157818306375, 'positive': 0.8097384218169362},\n",
       " {'negative': 0.19026157818306375, 'positive': 0.8097384218169362}]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "news_sentiment.predict_batch([negative_text,negative_text],get_proba=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      "  0%|          | 0/2.806271553039551 [00:00<?, ?MB/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "downloading pickled tfidf vectorizations\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "3MB [00:02,  1.45MB/s]                                       \n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'negative': 0.38166726, 'positive': 0.61833274}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "news_sentiment = malaya.pretrained_xgb_sentiment()\n",
    "news_sentiment.predict(negative_text,get_proba=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['bahdanau', 'attention', 'luong', 'normal']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sentiment_available_models = malaya.get_available_sentiment_models()\n",
    "sentiment_available_models"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Testing bahdanau model\n",
      "{'negative': 0.42894447, 'positive': 0.57105553, 'attention': [['kerajaan', 0.07550501], ['bencikan', 0.29057813], ['rakyatnya', 0.1474754], ['minyak', 0.48644146]]}\n",
      "\n",
      "Testing attention model\n",
      "{'negative': 0.43696418, 'positive': 0.56303585, 'attention': [['kerajaan', 0.26913235], ['bencikan', 0.38034844], ['rakyatnya', 0.28445157], ['minyak', 0.06606761]]}\n",
      "\n",
      "Testing luong model\n",
      "{'negative': 0.49942672, 'positive': 0.5005733, 'attention': [['kerajaan', 0.056799203], ['bencikan', 0.2996163], ['rakyatnya', 0.58679783], ['minyak', 0.056786623]]}\n",
      "\n",
      "Testing normal model\n",
      "{'negative': 0.48782662, 'positive': 0.5121734}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for i in sentiment_available_models:\n",
    "    print('Testing %s model'%(i))\n",
    "    news_sentiment = malaya.deep_sentiment(i)\n",
    "    print(news_sentiment.predict(negative_text))\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('tests/02032018.csv',sep=';')\n",
    "df = df.iloc[3:,1:]\n",
    "df.columns = ['text','label']\n",
    "corpus = df.text.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             precision    recall  f1-score   support\n",
      "\n",
      "   Negative       0.00      0.00      0.00         9\n",
      "    Neutral       0.67      0.08      0.15        24\n",
      "   Positive       0.47      0.96      0.63        28\n",
      "\n",
      "avg / total       0.48      0.48      0.35        61\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.6/site-packages/sklearn/metrics/classification.py:1135: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples.\n",
      "  'precision', 'predicted', average, warn_for)\n"
     ]
    }
   ],
   "source": [
    "dataset = [[df.iloc[i,0],df.iloc[i,1]] for i in range(df.shape[0])]\n",
    "bayes=malaya.bayes_sentiment(dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Positive'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bayes.predict(dataset[0][0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             precision    recall  f1-score   support\n",
      "\n",
      "     adidas       0.97      0.53      0.69       325\n",
      "      apple       0.98      0.54      0.70       488\n",
      "     hungry       0.79      0.91      0.85      1038\n",
      "   kerajaan       0.87      0.80      0.83      1380\n",
      "       nike       0.93      0.56      0.70       293\n",
      "pembangkang       0.69      0.88      0.78      1535\n",
      "\n",
      "avg / total       0.82      0.79      0.79      5059\n",
      "\n"
     ]
    }
   ],
   "source": [
    "bayes = malaya.bayes_sentiment('tests/local')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'pembangkang'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bayes.predict('saya suka kerajaan dan anwar ibrahim')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
